﻿using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _060.异步方法
{
	internal class Program
	{
		static async Task Main(string[] args)
		{
			// await DownloadAsync("https://www.baidu.com",@"d:\1.txt");
			// await Console.Out.WriteLineAsync("ok");

			Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
			StringBuilder sb = new StringBuilder();
			//for (int i = 0; i < 1000000; i++)
			//{
			//	sb.Append("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
			//}
			sb.Append("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
			await File.WriteAllTextAsync(@"d:\fzn.txt", sb.ToString());
			Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
		}

		/**
		 * 下载
		 */
		static async Task<int> DownloadAsync(string url, string destFilePath)
		{
			string body;
			using (HttpClient httpClient = new HttpClient())
			{
				body = await httpClient.GetStringAsync(url);
			}
			await File.WriteAllTextAsync(destFilePath, body);
			return body.Length;
		}


		/**
		 * 测试
		 */
		static async Task Test()
		{
			string fileName = "d:/1.txt";
			File.Delete(fileName);
			await File.WriteAllTextAsync(fileName, "hello async");
			string s = await File.ReadAllTextAsync(fileName);
			Console.WriteLine(s);
		}

	}
}
